package com.ddstation.hzsc_invs.autonomy_examination.controller;


import com.ddstation.common.util.DateUtil;
import com.ddstation.crm.institution.mapper.DtHealthManageInstitutionMapper;
import com.ddstation.crm.login.bean.SessionLoginData;
import com.ddstation.crud.entity.DtInvsAutonomyExaminationInstitutionDataEntity;
import com.ddstation.crud.entity.DtInvsManageInstitutionEntity;
import com.ddstation.crud.mapper.DtInvsManageInstitutionMapper;
import com.ddstation.hzsc_invs.autonomy_examination.service.DtInvsAutonomyExaminationInstitutionDataService;
import com.ddstation.util.getDateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自律自查
 * @author gqw
 * @date 2020-11-24
 */
@Controller
public class AutonomyExaminationController {
    @Inject
    private DtInvsAutonomyExaminationInstitutionDataService dtInvsAutonomyExaminationInstitutionDataService;
    @Inject
    private DtInvsManageInstitutionMapper dtInvsManageInstitutionMapper;
    @Inject
    private DtHealthManageInstitutionMapper dtHealthManageInstitutionMapper;
    private static final String INVS_INSTITUTION_ID = "00000007";

    /**
     * 日常自查页面
     * @param model
     * @param session
     * @return
     */
    @RequestMapping("invs_autonomy_examination_daily.do")
    public String autonomyExaminationDaily(Model model, HttpSession session,@RequestParam(required=false)String enforcementStatus){
        // 从session获取基础数据
        SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
        if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
            return "forward:login?timeout=true"; // session 过期
        }

        //取开始时间
        String firstday = getDateUtil.getFirstday(3);
        model.addAttribute("dtFrom", firstday);
        //结束时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        String lastday = format.format(cale.getTime());
        model.addAttribute("dtTo", lastday);
        if(StringUtils.isNotBlank(enforcementStatus)){
            model.addAttribute("enforcementStatus",enforcementStatus);
        }

        return "pc/autonomy_examination/autonomy_examination_daily/autonomy_examination_daily";
    }

    @RequestMapping("invs_autonomy_examination_overall.do")
    public String autonomyExaminationOverall(Model model, HttpSession session,@RequestParam(required=false)String enforcementStatus){
        // 从session获取基础数据
        SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY);
        if (loginData == null || loginData.getDoctor() == null || loginData.getInstitution() == null) {
            return "forward:login?timeout=true"; // session 过期
        }

        //取开始时间
        String firstday = getDateUtil.getFirstday(3);
        model.addAttribute("dtFrom", firstday);
        //结束时间
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar cale = Calendar.getInstance();
        String lastday = format.format(cale.getTime());
        model.addAttribute("dtTo", lastday);
        if(StringUtils.isNotBlank(enforcementStatus)){
            model.addAttribute("enforcementStatus",enforcementStatus);
        }

        return "pc/autonomy_examination/autonomy_examination_overall/autonomy_examination_overall";
    }


    /**
     * 未自查执法数据
     * @param name 诊所名
     * @param dtFrom 开始时间
     * @param dtTo  结束时间
     * @param type 类型 0：全面自查 1：日常自查
     * @param page
     * @param limit
     * @param enforcementSt 执法状态 0 未执法 1 已执法
     * @param session
     * @return
     */
    @RequestMapping("invs_autonomy_examination.do/load")
    public @ResponseBody
    Map<String, Object> loadAutonomyExaminationDaily(
            @RequestParam(required=false) String name,
            @RequestParam String dtFrom,
            @RequestParam String dtTo,
            @RequestParam Integer type,
            @RequestParam Integer page,
            @RequestParam Integer limit,
            @RequestParam Integer enforcementSt,
            HttpSession session){
        // 从session获取基础数据
        Map<String, Object> ret = new HashMap<String, Object>();
        ret.put("code", -1);
        ret.put("msg", "系统出错，请稍后再试！");

        SessionLoginData loginData = (SessionLoginData)session.getAttribute(SessionLoginData.KEY );
        DtInvsManageInstitutionEntity institution = loginData.getInstitution();
        if (loginData == null || loginData.getDoctor() == null || institution == null) {
            return ret;
        }

        Map<String,Object> params = new HashMap<String,Object>();
        params.put("limit",limit); //分页
        params.put("pageStart",page > 0 ? (page -1) * limit : 0);//分页
        params.put("institutionName",name); //机构名称
        params.put("dtFrom",dtFrom); //开始时间
        params.put("dtTo",dtTo); //结束时间
        params.put("enforcementSt",enforcementSt);
        params.put("type",type);
        params.put("province",institution.getProvince());
        params.put("city",institution.getCity());
        params.put("county",institution.getCounty());

        params.put("examinationSt",0);

        Integer count = dtInvsAutonomyExaminationInstitutionDataService.count(params);
        List<DtInvsAutonomyExaminationInstitutionDataEntity> list = dtInvsAutonomyExaminationInstitutionDataService.list(params);

        ret.put("code", 0);
        ret.put("msg", "");
        ret.put("count", count);
        ret.put("data", list);

        return ret;
    }

    @RequestMapping("invs_autonomy_examination_homePage.do/load")
    public @ResponseBody
    Map<String, Object> loadHomePageAutonomyExaminationData(
            @RequestParam Integer offset,
            @RequestParam Integer limit){
        Map<String, Object> ret = new HashMap<String, Object>();


        DtInvsManageInstitutionEntity dtInvsManageInstitutionEntity = dtInvsManageInstitutionMapper.selectByPrimaryKey(INVS_INSTITUTION_ID);
        Integer province = Integer.valueOf(dtInvsManageInstitutionEntity.getProvince());
        Integer city = Integer.valueOf(dtInvsManageInstitutionEntity.getCity());
        Integer county = Integer.valueOf(dtInvsManageInstitutionEntity.getCounty());

        int year = Integer.parseInt(DateUtil.getCurrentYear()); //年
        int month = Integer.parseInt(DateUtil.getCurrentMonth()); //月



        Map<String,Object> params = new HashMap<String,Object>();
        params.put("limit",limit); //分页
        params.put("pageStart",offset);//分页
        params.put("type",0);
        params.put("province",province);
        params.put("city",city);
        params.put("county",county);
        params.put("year",year-1);
        params.put("examinationSt",0);
        params.put("enforcementSt",0);
        List<DtInvsAutonomyExaminationInstitutionDataEntity> overallList = dtInvsAutonomyExaminationInstitutionDataService.list(params); //全面自查数据列表
        params.put("enforcementSt",null);
        params.put("examinationSt",1);
        int overllHaveExamination = dtInvsAutonomyExaminationInstitutionDataService.count(params);//已自查数量
        params.put("examinationSt",0);
        int overllNoExamination = dtInvsAutonomyExaminationInstitutionDataService.count(params);//未自查数量
        params.put("enforcementSt",0);
        int overllNoEnforcement =dtInvsAutonomyExaminationInstitutionDataService.count(params);//未执法数量
        params.put("enforcementSt",1);
        int overllHaveEnforcement =dtInvsAutonomyExaminationInstitutionDataService.count(params);//已执法数量

        int quarter = (month % 3 == 0 ? (month / 3) : (month / 3 + 1)); //季度
        //获取上个季度
        quarter--;
        if(quarter == 0){  //上个季度是否是去年
            year--;
            quarter = 4;
        }
        params.put("type",1);
        params.put("quarter",quarter);
        params.put("year",year);
        params.put("examinationSt",0);
        params.put("enforcementSt",0);
        List<DtInvsAutonomyExaminationInstitutionDataEntity>  dailyList= dtInvsAutonomyExaminationInstitutionDataService.list(params); //日常自查数据列表
        params.put("enforcementSt",null);
        params.put("examinationSt",1);
        int dailyHaveExamination = dtInvsAutonomyExaminationInstitutionDataService.count(params);//未自查数量
        params.put("examinationSt",0);
        int dailyNoExamination = dtInvsAutonomyExaminationInstitutionDataService.count(params);//未自查数量
        params.put("enforcementSt",0);
        int dailyNoEnforcement = dtInvsAutonomyExaminationInstitutionDataService.count(params);//未执法数量
        params.put("enforcementSt",1);
        int dailyHaveEnforcement = dtInvsAutonomyExaminationInstitutionDataService.count(params);//已执法数量


        int count = dtHealthManageInstitutionMapper.statisticsInstitutionReviewQuantity(province, city, county, null);//应查机构

        ret.put("overallList",overallList);
        ret.put("overllNoExamination",overllNoExamination);
        ret.put("overllHaveExamination",overllHaveExamination);
        ret.put("overllNoEnforcement",overllNoEnforcement);
        ret.put("overllHaveEnforcement",overllHaveEnforcement);
        ret.put("dailyList",dailyList);
        ret.put("dailyNoExamination",dailyNoExamination);
        ret.put("dailyHaveExamination",dailyHaveExamination);
        ret.put("dailyNoEnforcement",dailyNoEnforcement);
        ret.put("dailyHaveEnforcement",dailyHaveEnforcement);
        ret.put("count",count);

        ret.put("errorcode", 0);
        return ret;
    }
}
